<template>
  <div class="legend-box" :class="['legend-box', { 'legend-box-retract': !retract }]" :style="{ right: right, width: width }">
    <div class="search-box" title="查看图例" @click="show2 = true" v-show="!show2"></div>
    <el-icon class="el-icon-caret-left" @click="show2 = false" v-show="show2">
      <CaretLeft />
    </el-icon>
    <transition name="el-zoom-in-center">
      <div class="station-module" v-show="show2">
        <div class="station-module-main">
          <div class="sousuojieguo-box">
            <div>地图图例</div>
            <button v-if="isDensity" class="density-legend-btn" @click="$emit('show-density-legend')">查看密度图例</button>
          </div>
          <div class="station-item" v-for="(item, index) in legend" :key="index">
            <img class="station-item-icon-box" :src="item.url" v-if="item.url" />
            <template v-else-if="item.color">
              <div class="station-item-checkbox-box">
                <el-checkbox v-model="item.checked" @change="addOrRemove(item.type)"></el-checkbox>
                <div class="station-item-color-box" :style="{ background: item.color }"></div>
              </div>
            </template>
            <div class="station-item-font-box">{{ item.name }}</div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';

interface LegendItem {
  url?: string;
  color?: string;
  checked?: boolean;
  type?: string;
  name: string;
}

const props = defineProps({
  legend: {
    type: Array as () => LegendItem[],
    required: true
  },
  type: {
    type: String,
    default: ''
  },
  width: {
    type: String,
    default: '300px'
  },
  flag: {
    type: Boolean,
    default: false
  },
  retract: {
    type: Boolean,
    default: true
  },
  isDensity: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(['addOrRemove', 'show-density-legend']);

const show2 = ref(false);
const right = ref('25%');

watch(
  () => props.flag,
  (newVal) => {
    if (newVal) {
      show2.value = true;
    }
  },
  { immediate: true }
);

// Clear or add range
const addOrRemove = (type: string) => {
  emit('addOrRemove', type);
};
</script>

<style lang="scss" scoped>
::-webkit-scrollbar {
  display: none;
}

.legend-box {
  z-index: 99;
  position: absolute;
  bottom: 10%;
  color: #fff;
  width: 300px;

  .search-box {
    position: absolute;
    right: 10%;
    bottom: 0px;
    font-size: 20px;
    cursor: pointer;
    width: 30px;
    height: 30px;
    background: url(@/assets/largescreen/legend.png);
    background-size: cover;
  }

  .el-icon-caret-left {
    position: absolute;
    left: -46px;
    top: 10px;
    font-size: 20px;
    cursor: pointer;
  }

  .station-module {
    position: relative;
    width: 100%;
    margin-top: 10px;
    margin-left: -26px;

    .station-module-main {
      padding: 0.9375rem;
      box-sizing: border-box;
      width: 100%;
      max-height: 20rem;
      background-color: rgba(6, 48, 68, 0.3);
      overflow: auto;

      .sousuojieguo-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        color: #24f2fe;
        padding: 4px 0;
        box-sizing: border-box;
        border-bottom: 1px solid #00ffff;
      }

      .station-item {
        position: relative;
        font-family: YouSheBiaoTiHei;
        width: 100%;
        min-height: 1.875rem;
        background: rgba(14, 89, 214, 0.34);
        margin-top: 0.625rem;
        line-height: 1.875rem;
        padding: 0 10px;
        box-sizing: border-box;
        font-size: 1.125rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
        cursor: pointer;

        .station-item-icon-box {
          width: 20px;
          height: 25px;

          img {
            width: 100%;
            height: 100%;
          }
        }

        .station-item-checkbox-box {
          width: 30%;
          height: 25px;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .station-item-color-box {
            width: 20px;
            height: 20px;
          }
        }

        .station-item-font-box {
          color: #00ffff;
          font-family: PangMenZhengDao;
        }
      }
    }
  }
}

.legend-box-retract {
  right: 1% !important;
}

.legend-float {
  position: absolute;
  top: 10%;
  right: 10%;
  z-index: 1000;
}

.density-legend-btn {
  margin-left: 12px;
  background: #0e59d6;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 2px 10px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
}
.density-legend-btn:hover {
  background: #24f2fe;
  color: #0e59d6;
}
</style>
